package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.consatnts.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.*;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

/**
 * author by itheima
 *
 * @Date 2022/3/9
 * Description
 */
@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Autowired
    MongoTemplate mongoTemplate;
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    RedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {

        //   1. 判断用户是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        //未登录 直接将 4种行为 均返回false
        ArticleResponse articleResponse = new ArticleResponse();
        if (user == null) {
            articleResponse.setIscollection(false);
            articleResponse.setIsfollow(false);
            articleResponse.setIslike(false);
            articleResponse.setIsunlike(false);
            return ResponseResult.okResult(articleResponse);
        }

        //   3. 如果登录
        //   4. 查询行为实体
        ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(),null);

        Query query = Query.query(Criteria.where("articleId").is(dto.getArticleId()).and("entryId").is(apBehaviorEntry.getId()));
        //   5. 根据行为实体 文章ID查询 是否点赞
        ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
        if(apLikesBehavior!=null){
           // Short operation = apLikesBehavior.getOperation();
                articleResponse.setIslike(true);

        }

        //  6. 根据行为实体 文章ID查询 是否不喜欢
        ApUnlikesBehavior apUnlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
        if(apUnlikesBehavior!=null){
          //  Short type = apUnlikesBehavior.getType();

                articleResponse.setIsunlike(true);

        }

        //  7. 根据行为实体 文章ID查询 是否收藏
        ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
        if(apCollection!=null){
           // Short type1 = apCollection.getType();

                articleResponse.setIscollection(true);

        }

        //   8. 更具登录用户id 去redis中查询是否关注该作者
//        ZSetOperations<String,String> zSetOperations = redisTemplate.opsForZSet();
//        Double score = zSetOperations.score(UserRelationConstants.FOLLOW_LIST + user.getId(), String.valueOf(dto.getAuthorApUserId()));
//        if(score!=null){
//            articleResponse.setIsfollow(true);
//        }
        Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST+user.getId(),String.valueOf(dto.getAuthorApUserId()));
        if (score!=null){
            articleResponse.setIsfollow(true);
        }
        //  9. 封装结果 返回
        return ResponseResult.okResult(articleResponse);
    }
}
